package app.mvp.base.mvp;

import android.content.Context;

/**
 * Created by 刘智强 on 2017/4/6.
 */

public  abstract class BaseMvpPresenter<V extends BaseView> implements  BasePresenter<V> {
    private  V mvpView;
    public   Context context;

    @Override
    public void attachView(V view,Context context) {
        this.mvpView=view;
        this.context=context;

    }

    @Override
    public void detachView() {
        this.mvpView=null;

    }



    /**
     * 判断 view是否为空
     * @return
     */
    public  boolean isAttachView(){
        return mvpView != null;
    }

    /**
     * 返回目标view
     * @return
     */
    public  V getMvpView(){
        return mvpView;
    }

    public  abstract  void init();

    /**
     * 检查view和presenter是否连接
     */
    public void checkViewAttach(){
        if(! isAttachView()){
            throw  new MvpViewNotAttachedException();
        }
    }

    /**
     * 自定义异常
     */
    public static   class  MvpViewNotAttachedException extends RuntimeException{
        public  MvpViewNotAttachedException(){
            super("请求数据前请先调用 attachView(MvpView) 方法与View建立连接");
        }
    }

    /**
     * 从缓存中获取数据
     */
    public  abstract  void getDataFromCache();

    /**
     * 从服务器获取数据
     */
    public abstract  void getDataFromNet();
}
